package com.leenleda.ips.util.print;

import android.content.Context;
import android.util.Log;

import com.leenleda.ips.BaseActivity;
import com.szsicod.print.escpos.PrinterAPI;
import com.szsicod.print.io.InterfaceAPI;
import com.szsicod.print.io.USBAPI;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class NewPrintUtil {

    private String tag = this.getClass().getName();
    private static NewPrintUtil mInstance;

    public static NewPrintUtil getInstance(BaseActivity context) {
        if (mInstance == null) {
            synchronized (NewPrintUtil.class) {
                if (mInstance == null) {
                    mInstance = new NewPrintUtil();
                    mInstance.init(context);
                }
            }
        }
        return mInstance;
    }

    public PrinterAPI mPrinter = PrinterAPI.getInstance();
    private void init(Context context){
        if (mPrinter.isConnect()) {
            mPrinter.disconnect();
        }

        InterfaceAPI io = null;
        io = new USBAPI(context);
        int state = mPrinter.connect(io);
        Log.e(tag, "=========state========" + state);
    }

    private int cutter = 1;       // 默认0，  0 全切、1 半切
    public String printerTest(String cmdData, int cutter) {
        if (!isConnect()) {
            return "";
        }

        String state = StatusDescribe.getStatusDescribe(mPrinter.getStatus());
        Log.e(tag, "======get_state=======" + state );
        try {
            JSONArray array = new JSONArray(cmdData);
            JSONObject object = null;
            Log.e("LDNVDSSD", "==array=====" + array);
            for (int i = 0; i < array.length(); i++) {
                object = array.getJSONObject(i);
                int type = object.getInt("type");
                Log.e("LDNVDSSD", "--type-------" + type);
                if (type == 0) { //切纸
                    cut(cutter == 1 ? false : true);
                    continue;
                }
                String dataStr = "";

                int dataPosition = object.getInt("dataPosition");
                dataStr = object.getString("dataStr");
                mPrinter.setAlignMode(0);
//                mUsbDriver.write(PrintCmd.SetAlignment(dataPosition));
                Log.e("LDNVDSSD", "==dataStr===" + dataStr);
                mPrinter.setAlignMode(dataPosition);
                switch (type) {
                    case 1: //打印空行
                        int num = object.getInt("size");
                        Log.e("LDNVDSSD", "==num===" + num);
                        for (int m = 0; m < num; m ++){
                            mPrinter.printFeed();   //  打印并换行(空行)
                        }
                        break;
                    case 2://打印文字
                    case 3://换行打印文字
                        int dataSize = object.getInt("dataSize");
                        if (dataSize < 0 || dataSize > 7)
                            dataSize = 2;
                        boolean dataBold = object.getBoolean("dataBold");

                        mPrinter.setCharSize(dataSize,dataSize);    //0,1,2,3,4,5 (最大) 6,7
                        mPrinter.setEmphasizedMode(dataBold ? 1 : 0);
                        mPrinter.printString(dataStr, "GBK", type == 3 ? true : false);  //******
                        break;
                    case 4://打印条形码
                        int width = object.getInt("width");
                        int height = object.getInt("height");
                        mPrinter.setBarCodeWidth(width);
                        mPrinter.setBarCodeHeight(height);
                        mPrinter.printBarCode(type, dataStr.length(), dataStr);   //69 长一点   73 短一点
                        break;
                    case 5: //打印二维码
                        int size = object.getInt("size");
                        mPrinter.printQRCode(dataStr, size, false);
                        break;
                }
            }

            cut(cutter == 1 ? false : true);
//            mUsbDriver.write(PrintCmd.PrintMarkcutpaper(cutter)); // 不干胶（黑标）切纸
        } catch (JSONException | UnsupportedEncodingException e) {
            Log.e("LDNVDSSD", "--e-------" + e);
            e.printStackTrace();
        }
        Log.e("LDNVDSSD", "--打印完成-------");
        return "";
    }

    public boolean isConnect() {
        boolean isConnect = true;
        if (!mPrinter.isConnect()) {
            isConnect = false;
        }
        return isConnect;
    }

    private void cut(boolean full){
        if (full){
            mPrinter.fullCut();
            return;
        }
        try {
            Thread.sleep(100);
            mPrinter.cutPaper(66, 0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
